package com.ldatum.crm.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ldatum.crm.dao.TEmployeeDao;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.util.HibernateUtil;
import com.ldatum.crm.util.MD5;

public class TEmployeeDaoImpl implements TEmployeeDao {

	@Override
	public void add(TEmployee employee) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.save(employee);
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public void delete(TEmployee employee, int id) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.delete(employee);
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public void update(TEmployee employee, int id) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.update(employee);
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public TEmployee findById(Class<TEmployee> clazz,int id) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			return (TEmployee) s.get(clazz, id);
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public TEmployee findByLoginname(Class<TEmployee> clazz, String loginname)
			throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TEmployee as employee where loginname = :loginname";
			Query q = s.createQuery(hql);
			q.setString("loginname", loginname);
			TEmployee emp = (TEmployee) q.uniqueResult();
//			Hibernate.initialize(emp.getPosition());
//			Hibernate.initialize(emp.getDepartment());
			return emp;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}

	@Override
	public List<TEmployee> list(Class<TEmployee> clazz) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TEmployee as employee where department_id !=6 order by fullname ASC";
			Query q = s.createQuery(hql);
			@SuppressWarnings("unchecked")
			List<TEmployee> list = (List<TEmployee>) q.list();
			return list;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}

	@Override
	public void save(TEmployee employee) throws Exception {
		HibernateUtil.add(employee);
	}

	@Override
	public void deletes(String ids) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			String sql = "delete from t_employee where id in("+ids+")";
			Query q = s.createSQLQuery(sql);
			q.executeUpdate();
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}

	}

	@Override
	public void resetPasswd(String ids,String npwd) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			String sql = "update t_employee set password =:password where id in("+ids+")";
			Query q = s.createSQLQuery(sql);
			q.setString("password", npwd);
			q.executeUpdate();
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public void changepwd(Integer id, String newpwd) throws Exception {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			String sql = "update t_employee set password =:password where id = "+id;
			Query q = s.createSQLQuery(sql);
			q.setString("password", MD5.md5(newpwd));
			q.executeUpdate();
			tx.commit();
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public String findPwdById(Integer id) throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "select password from t_employee where id = :id";
			Query q = s.createQuery(hql);
			q.setInteger("id", id);
			TEmployee emp = (TEmployee) q.uniqueResult();
			return emp.getPassword();
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
	
}
